package com.bbjob.job;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import com.bbjob.client.SocketUser;

public class ServerThreadDeal extends Thread {
    private SocketUser user;
    // private List<SocketUser> list;
    private Map<String, SocketUser> map;
    
    public ServerThreadDeal(SocketUser user, Map<String, SocketUser> map) {
        this.user = user;
        this.map = map;
    }
 
    public void run() {
        try {
            while (true) {
                // 信息的格式：(add||remove||chat),收件人,...,收件人,发件人,信息体
                //不断地读取客户端发过来的信息
                String msg = user.getBr().readLine();
                System.out.println(msg);
                String[] str = msg.split(",");
                // int i=str.length;
                // System.out.println(i);
                // 转发消息
                switch (str[0]) {
                    case "information":
                    	
                    	addDetailInfo(str, user, map);
                        break;
                    case "chat":
                        break;
                    case "add":
                    	break;
                    default:
                        break;
                }
            }
        } catch (Exception e) {
            System.out.println("消息回复异常" + new Date());
            map.remove(user.getSocket().getInetAddress().getHostAddress());
            // list.remove(user);
        } finally {
            try {
                user.getBr().close();
                user.getSocket().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

	private void addDetailInfo(String[] str, SocketUser user2,
			Map<String, SocketUser> socketMap) {
		
		String ip = user2.getSocket().getInetAddress().getHostAddress();
		
		if (socketMap.get(ip) != null) {
			SocketUser item = socketMap.get(ip);
			item.setcIp(ip);
			item.setExamId(Integer.valueOf(str[1]));
			item.setqId(str[2]);
		}
	}
}
